
cc.Class({
    extends: cc.Component,

    properties: {
        bg_sp: {
            default: null,
            type: cc.Sprite,
            displayName: '面板背景Sprite'
        },
        head_sp: {
            default: null,
            type: cc.Sprite,
            displayName: '图片信息Sprite'
        },
        frame: {
            default: null,
            type: cc.Sprite,
            displayName: '框Sprite'
        },
        lv_SP: {
            default: null,
            type: cc.Sprite,
            displayName: '攻击类型Sprite'
        },
        num_lab: {
            default: null,
            type: cc.Label,
            displayName: '数量label'
        },
        progress_barBg: {
            default: null,
            type: cc.Sprite,
            displayName: '进度条背景Sprite'
        },
        progress_bar: {
            default: null,
            type: cc.Node,
            displayName: '进度条Node'
        },
        progress_barFrame: {
            default: null,
            type: cc.Sprite,
            displayName: '进度条的框Sprite'
        },
        dq_node: {
            default: null,
            type: cc.Node,
            displayName: '‘当前’提示Node'
        },
        starNode: {
            default: null,
            type: cc.Node,
            displayName: '星星Node'
        },
        progress_node: {
            default: null,
            type: cc.Node,
            displayName: '进度条Node'
        },
        lock_node: {
            default: null,
            type: cc.Node,
            displayName: '锁Node'
        },
        info_lab: {
            default: null,
            type: cc.Label,
            displayName: '信息lab'
        },
        //检测长按时间
        holdTime: {
            default: 0,
            visible: false
        },
        //检测点击
        holdClick: {
            default: false,
            visible: false
        },
        //检测双击
        doubleClick: {
            default: 0,
            visible: false
        },
        //检测单击
        hold_one_click: {
            default: 0,
            visible: false
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
    },
    /**
     * 展示item数据
     * 图片资源会更替，暂时不写死
     */
    initInfo(info) {
        if (!info) return
        this.oneLen = 108 / 3
        this.dq_node.active = false
      
    },
    /**
     * 如果所有的bg，icon,frame都按一定的规则放置在同一个plist，可以这样操作
     * @param {*} bgName 
     * @param {*} iconName 
     * @param {*} frameName 
     * @param {*} scale 
     */
    setItemInfo(bgName, iconName, frameName, scale) {
        if (bgurl) {
            //格式如下
            this.bg_sp.spriteFrame = he.load.get('url', cc.SpriteAtlas).getSpriteFrame(iconName)
        }
    },



    //炮塔初始化
    towerInfoInit() {
        // this.bg_sp.spriteFrame=
        // this.head_sp.spriteFrame=
        // this.frame.spriteFrame= 
        this.progress_node.active = false
        this.setStarNum(this.info.level)
    },

    //角色初始化
    roleInfoInit() {
        // this.bg_sp.spriteFrame=
        // this.head_sp.spriteFrame=
        // this.frame.spriteFrame=
        this.progress_node.active = true
        let barSp = this.progress_bar.getComponent(cc.Sprite)
        // barSp.spriteFrame=
        // this.progress_barBg.spriteFrame=
        // this.progress_barFrame.spriteFrame=
        // this.lv_SP.spriteFrame=
        this.setStarNum(this.info.level)
        //bar的长度  和 数量 暂不确定
    },

    // 合成书初始化
    bookInfoInit() {
        // this.bg_sp.spriteFrame=
        // this.head_sp.spriteFrame=
        // this.frame.spriteFrame= 
        this.progress_node.active = false
        this.starNode.active = false
    },


    //当前的框显示
    setDq() {
        if (this.dq_node) {
            this.dq_node.active = true
        } else {
            console.log('框未绑定')
        }
    },
    //关闭
    closeDq() {
        if (this.dq_node) {
            this.dq_node.active = false
        } else {
            console.log('框未绑定')
        }
    },
    //锁了
    showLock() {
        if (!this.lock_node) {
            console.log('未绑定锁')
            return
        } else {
            this.lock_node.active = true
        }
    },

    //解锁了
    closeLock() {
        if (!this.lock_node) {
            console.log('未绑定锁')
            return
        } else {
            this.lock_node.active = false
        }
    },
    /**
     * 
     * @param {*} size 字体大小
     * @param {*} pos lab的位置
     * @param {*} color 字体颜色
     */
    setLabInfo(size, pos, color) {
        if (!this.info_lab) {
            console.log('lab未绑定')
            return
        } else {
            let lab = this.info_lab.node
            lab.active = true
            if (size) {
                this.info_lab.fontSize = size
            }
            if (pos) {
                lab.setPosition(pos)
            }
            if (color) {
                lab.setColor(color)
            }
        }
    },

    //进度条初始化 
    //数据结构会更改，暂时都为0
    /**
     * 
     * @param {*} num 为总的数量
     */
    setProgress(num) {
        if (!this.progress_node) {
            console.log('未绑定进度条节点')
            return
        } else {
            if (this.progress_bar) {
                this.progress_bar.width = this.oneLen * num
            }
            if (this.num_lab) {
                this.num_lab.string = `${num}/${3}`
            }
        }
    },
    //进度条更改动画
    progressChange(num) {
        if (this.barChangeTween) return
        this.barChangeTween = cc.tween(this.progress_bar)
            .to(0.15, { width: num * this.oneLen }, { easing: 'sineOutIn' })
            .call(() => {
                this.num_lab.string = `${num}/${3}`
            })
            .start()
    },


    //设置星星数量
    setStarNum() {
        if (!this.starNode) {
            console.log('未绑定星星节点')
            return
        } else {    
            for (let j = 0; j < this.starNode.children.length; j++) {
                this.starNode.children[j].active = false
            }
            if (this.info.level) {
                for (let i = 0; j < this.info.level; i++) {
                    this.starNode.children[i].active = true
                }
            }
        }
    },

    test() {
        this.initInfo(22)
        this.setProgress(1)
        this.scheduleOnce(() => {
            this.progressChange(2)
        }, 1)
    },

    // 开启监听，如果是拖动的touch事件 需要自行重新设计，不用开启该监听
    openButtonListen(startfunc, oncefunc, doublefunc, longfunc) {
        if (startfunc) {
            this.startFunc = startfunc
        }
        if (oncefunc) {
            this.onceFunc = oncefunc
        }
        if (doublefunc) {
            this.doubleFunc = doublefunc
        }
        if (longfunc) {
            this.longFunc = longfunc
        }

        this.node.on(cc.Node.EventType.TOUCH_START, function (event) {
            this.holdClick = true;
            this.holdTime = 0;
        }, this);

        this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
            this.holdClick = false;
            if (this.holdTime >= 60) {
                this.btn_status('long');
            }
            else {
                this.btn_status('short');
            }
            //开始记录时间
            this.holdTime = 0;
        }, this);
    },
    btn_status(status) {
        if (status == 'short') {
            this.hold_one_click++;
            this.setTime = setTimeout(() => {
                if (this.hold_one_click == 1 && this.onceFunc) {
                    console.log('short');
                    this.onceFunc()
                    this.hold_one_click = 0;
                }
                else if (this.hold_one_click == 2 && this.doubleFunc) {
                    console.log('double');
                    this.doubleFunc()
                    this.hold_one_click = 0;
                }
            }, 400);
        }
        else {
            this.hold_one_click = 0;
            if (this.longFunc) {
                this.longFunc()
            }
            console.log(status);
        }
    },

    update(dt) {
        if (this.holdClick) {
            this.holdTime++;
            if (this.holdTime > 120)//如果长按时间大于2s，则认为长按了2s
            {
                this.holdTime = 120;
            }
        }
    },

    onDestroy() {
        this.node.targetOff(this.node)
        if (this.setTime) {
            clearTimeout(this.setTime)
            this.setTime = null
        }
        if (this.barChangeTween) {
            this.barChangeTween.stop()
            this.barChangeTween = null
        }
    },
});
